//
//  FANOriginalView.swift
//  Weibo - Swift
//
//  Created by user on 16/4/20.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class FANOriginalView: UIView {

    var bottomCons:Constraint?
    
    var dataViewModel:FANDataViewModel?{
        
        didSet{
            
            iconImageView.sd_setImageWithURL(NSURL(string: (dataViewModel?.status?.user?.profile_image_url)!), placeholderImage:  UIImage(named: "avatar_default_small"))
            
            memberImageView.image = dataViewModel?.memberImage
            
            nameLabel.text = dataViewModel?.status?.user?.name
            
//            timeLabel.text = dataViewModel?.status?.created_at
            
            timeLabel.text = dataViewModel?.createText
            
            sourseLabel.text = dataViewModel?.sourceText
            
//            contentLabel.text = dataViewModel?.status?.text
            
            contentLabel.attributedText = dataViewModel?.originalAttributeString
            
            bottomCons?.uninstall()
            
            if let pic_urls = dataViewModel?.status?.pic_urls where pic_urls.count > 0{
                
//                pictureView.snp_updateConstraints(closure: { (make) -> Void in
//                    
//                    make.size.equalTo(dataViewModel?.originalPictureSize ?? CGSizeZero)
//                    
//                })
                
                pictureView.dataInfo = (pic_urls,dataViewModel!.originalPictureSize)
                
                pictureView.hidden = false
                
//                pictureView.pic_urls = pic_urls
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(FANMargin).constraint
                })
                
            }else{
                
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(FANMargin).constraint
                })
                
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourseLabel)
        addSubview(contentLabel)
        addSubview(VimageView)
        addSubview(pictureView)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(FANMargin)
            
            make.width.height.equalTo(FANIconImageViewWH)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(iconImageView.snp_trailing).offset(FANMargin)
            
            make.top.equalTo(iconImageView)
        }
        
        memberImageView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLabel.snp_trailing).offset(FANMargin)
            
            make.centerY.equalTo(nameLabel).offset(-2)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(iconImageView.snp_trailing).offset(FANMargin)
            
            make.bottom.equalTo(iconImageView)
        }
        
        sourseLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(FANMargin)
            
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(FANMargin)
            
            make.top.equalTo(iconImageView.snp_bottom).offset(FANMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(FANMargin)
            
            make.leading.equalTo(contentLabel.snp_leading)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(FANMargin).constraint
        }
        
        VimageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImageView.snp_trailing).offset(-2)
            
            make.centerY.equalTo(iconImageView.snp_bottom).offset(-2)
        }
        
    }
    
    
    private lazy var iconImageView:UIImageView = {
        
        let iconImageView = UIImageView()
        
        iconImageView.image = UIImage(named: "avatar_default_small")
        
        return iconImageView
    
    }()
    
    private lazy var nameLabel:UILabel = {
    
        let namelabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: FANNameLabelFontSize)
        
        namelabel.text = "哈哈哈"
        
        return namelabel
    
    }()
    
    private lazy var memberImageView:UIImageView = {
    
        let memberImageView = UIImageView()
        
        memberImageView.image = UIImage(named: "common_icon_membership")
        
        return memberImageView
    }()
    
    private lazy var timeLabel:UILabel = {
    
        let timeLabel = UILabel(textColor: UIColor.orangeColor(), fontSize: FANTimeLabelFontSize)
        
        timeLabel.text = "刚刚"
        
        return timeLabel
        
    }()
    
    private lazy var sourseLabel:UILabel = {
    
        let sourseLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: FANSourseLabelFontSize)
        
        sourseLabel.text = "来自 iphone"
        
        return sourseLabel
    }()
    
    private lazy var contentLabel:YYLabel = {
    
        let contentLabel = YYLabel()
        
        /*textColor: UIColor.darkGrayColor(), fontSize: FANStatusCellContentFontSize, preferredMaxLayoutWidth: ScreenW - 2 * FANMargin*/
        
        contentLabel.textColor = UIColor.darkGrayColor()
        
        contentLabel.font = UIFont.systemFontOfSize(FANStatusCellContentFontSize)
        
        contentLabel.preferredMaxLayoutWidth = ScreenW - 2 * FANMargin
 
        contentLabel.userInteractionEnabled = true
        
        return contentLabel
    
    }()
    
    private lazy var VimageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var pictureView:FANPictureCollectionView = {
    
        let pictureView = FANPictureCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
        return pictureView
    }()
}
